//
//  FR_StudentRecordList_ViewController.m
//  FaceRecognition
//
//  Created by Sylar on 2016/12/6.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "FR_StudentRecordList_ViewController.h"
#import "FR_Record_InformationViewController.h"

#import "FR_HistoryRecordCell.h"
#import "FR_HistoryRecord_Model.h"



@interface FR_StudentRecordList_ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    
}
@property (nonatomic, strong) UITableView *recordTable;
@property (nonatomic, strong) NSMutableArray *recordArr;
@property (nonatomic, strong) UILabel      *nodataLabel;
@property (nonatomic, assign) NSInteger pageNum;

@end

@implementation FR_StudentRecordList_ViewController

- (void)loadView {
    [super loadView];
    
    self.navBar.nav_titleLabel.text = @"识别记录";
    
    [self.view addSubview:self.recordTable];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self getRecordListData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Request

- (void)getRecordListData {
    
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc]init];
    
    [paramDic setObject:@"20" forKey:@"pagesize"];
    [paramDic setObject:@"1" forKey:@"pageno"];
    if (self.recordType == 0) {
        [paramDic setObject:self.classId forKey:@"classid"];
        [paramDic setObject:self.userid forKey:@"studentid"];
        [paramDic setObject:[SYAccountTool ShareAccount].account.scid forKey:@"scid"];
    }else {
        [paramDic setObject:self.scid forKey:@"scid"];
        [paramDic setObject:self.userid forKey:@"userid"];
    }
    
    NSString *methodStr = self.recordType == 0 ? @"api/getStuParEventHis":@"api/getEmployeeEventHis";
    
    [Utils Java_get:methodStr parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        self.pageNum = 1;//重置分页
        
        if (arr.count > 0) {
            self.recordArr = [[NSMutableArray alloc]init];
            
            for (NSDictionary *dic in arr) {
                
                FR_HistoryRecord_Model *model = [FR_HistoryRecord_Model mj_objectWithKeyValues:dic];
                
                [self.recordArr addObject:model];
            }
            self.nodataLabel.hidden = YES;
        }else {
            self.nodataLabel.hidden = NO;
        }
        
        [self.recordTable.mj_footer endRefreshing];
        [self.recordTable.mj_header endRefreshing];
        [self.recordTable reloadData];
        
    }failure:^(NSInteger recode, NSError *error) {
        [self showHint:error.localizedDescription];
        [self.recordTable.mj_header endRefreshing];
    }];
    
    
}

- (void)loadMoreRecord {
    
    self.pageNum += 1;
    
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc]init];
    
    [paramDic setObject:@"20" forKey:@"pagesize"];
    [paramDic setObject:[NSString stringWithFormat:@"%ld",(long)self.pageNum] forKey:@"pageno"];
    if (self.recordType == 0) {
        [paramDic setObject:self.classId forKey:@"classid"];
        [paramDic setObject:self.userid forKey:@"studentid"];
        [paramDic setObject:[SYAccountTool ShareAccount].account.scid forKey:@"scid"];
    }else {
        [paramDic setObject:self.scid forKey:@"scid"];
        [paramDic setObject:self.userid forKey:@"userid"];
    }
    
    NSString *methodStr = self.recordType == 0 ? @"api/getStuParEventHis":@"api/getEmployeeEventHis";
    
    [Utils Java_get:methodStr parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count > 0) {
            
            for (NSDictionary *dic in arr) {
                
                FR_HistoryRecord_Model *model = [FR_HistoryRecord_Model mj_objectWithKeyValues:dic];
                
                [self.recordArr addObject:model];
            }
            
            [self.recordTable.mj_footer endRefreshing];
        }else {
            self.pageNum -= 1;
            [self.recordTable.mj_footer endRefreshingWithNoMoreData];
            
        }
        
        [self.recordTable reloadData];
        
    }failure:^(NSInteger recode, NSError *error) {
        self.pageNum -= 1;
        [self.recordTable.mj_footer endRefreshing];
    }];
    
    
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.recordArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FR_HistoryRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hisIdentifier"];
    if (cell == nil) {
        cell = [[FR_HistoryRecordCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"hisIdentifier"];
    }
    
    FR_HistoryRecord_Model *model = [self.recordArr objectAtIndex:indexPath.row];
    
    [cell creatCell:model object:2];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    FR_Record_InformationViewController *infoVC = [[FR_Record_InformationViewController alloc]init];
    FR_HistoryRecord_Model *model = [self.recordArr objectAtIndex:indexPath.row];
    infoVC.recordType = self.recordType;
    infoVC.recordModel = model;
    [self.navigationController pushViewController:infoVC animated:YES];
    
}

#pragma mark - Get 

- (UITableView *)recordTable {
    
    if (_recordTable == nil) {
     
        _recordTable = [[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH - SafeAreaTopHeight - SafeAreaBottomHeight) style:(UITableViewStylePlain)];
        _recordTable.delegate = self;
        _recordTable.dataSource = self;
        _recordTable.rowHeight = 58;
        _recordTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        _recordTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self getRecordListData];
        }];
        _recordTable.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            [self loadMoreRecord];
        }];
        
        self.nodataLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 150, SCREENWIDTH, 15)];
        [self.nodataLabel setFont:[UIFont systemFontOfSize:14]];
        [self.nodataLabel setText:@"暂无识别记录"];
        [self.nodataLabel setTextColor:[UIColor colorWithHexString:@"#979797"]];
        self.nodataLabel.textAlignment = NSTextAlignmentCenter;
        self.nodataLabel.hidden = YES;
        
        [_recordTable addSubview:self.nodataLabel];
        
    }
    return _recordTable;
}

@end
